local skel = fk.CreateSkill {
  name = "efengqi__shaoyan",
}

Fk:loadTranslationTable{
  ["efengqi__shaoyan"] = "韶颜",
  [":efengqi__shaoyan"] = "当一张判定牌生效前，你可以用一张“琴”替换之；每轮限一次，当你成为手牌数大于你的角色使用牌的目标后，其须将一张手牌标记为“琴”。",

  ["#efengqi__shaoyan-card"] = "韶颜：你须将一张手牌标记为“琴”",
  ["#efengqi__shaoyan-ask"] = "韶颜：你可以用一张“琴”替换 %dest “%arg”的判定牌%arg2",

  ["$efengqi__shaoyan1"] = "",
  ["$efengqi__shaoyan2"] = "",
}

skel:addEffect(fk.TargetConfirmed, {
  anim_type = "control",
  times = function (self, player)
    return 1 - player:usedEffectTimes(self.name, Player.HistoryRound)
  end,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    return target == player and data.from and data.from:getHandcardNum() > player:getHandcardNum()
      and player:usedEffectTimes(self.name, Player.HistoryRound) == 0
  end,
  on_cost = function(self, event, target, player, data)
    event:setCostData(self, { tos = {data.from} })
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = data.from
    local cards = room:askToCards(to, {
      min_num = 1, max_num = 1, skill_name = skel.name, include_equip = false, cancelable = false, pattern = ".|.|.|hand",
      prompt = "#efengqi__shaoyan-card"
    })
    if #cards == 0 then return end
    room:setCardMark(Fk:getCardById(cards[1]), "@@efengqi__qiqin", 1)
    local record = room:getBanner("efengqi__qiqin_record") or {}
    table.insertIfNeed(record, cards[1])
    room:setBanner("efengqi__qiqin_record", record)
  end,
})

skel:addEffect(fk.AskForRetrial, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(skel.name) then return false end
    return not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    local ids = table.filter(player:getCardIds("he"), function(id) return Fk:getCardById(id):getMark("@@efengqi__qiqin") ~= 0 end)
    local cards = player.room:askToCards(player, {
      min_num = 1, max_num = 1, skill_name = skel.name, include_equip = true, pattern = tostring(Exppattern{ id = ids }),
      prompt = "#efengqi__shaoyan-ask::"..target.id..":"..data.reason..":"..data.card:toLogString()
    })
    if #cards > 0 then
      event:setCostData(self, { tos = {target}, cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:changeJudge{
      card = Fk:getCardById(event:getCostData(self).cards[1]),
      player = player,
      data = data,
      skillName = skel.name,
      exchange = true,
    }
  end,
})

return skel
